package com.xu.createThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Thread4 {

    static class MyCallable implements Callable{
        @Override
        public Object call() throws Exception {
            int count = 0;
            for (int i = 0; i < 100; i++) {
                count+=i;
            }
            return count;
        }
    }

    /**
     * FatureTask 一般用于有返回结果的非阻塞的执行方法同步非阻塞
     * @param args
     * @throws ExecutionException
     * @throws InterruptedException
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //1:创建myCallable对象
        MyCallable myCallable = new MyCallable();
        //2：创建FutureTask 传入Callable
        FutureTask task = new FutureTask(myCallable);
        //3创建Thread线程
        Thread t = new Thread(task);
        //4：启动线程
        t.start();
        //5：获取结果值
        Object o = task.get();
        System.out.println(o);
    }
}
